package com.cobrapi.movy.dao;

import java.util.Collection;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import br.com.caelum.vraptor.ioc.Component;

import com.cobrapi.movy.dao.common.GenericDao;
import com.cobrapi.movy.model.Filme;
import com.cobrapi.movy.repository.FilmeRepository;

@Component
public class FilmeDao extends GenericDao<Filme> implements FilmeRepository {

	private EntityManager manager;
	
	public FilmeDao(EntityManager manager){
		super(manager);
	}
	
	@Override
	public Collection<Filme> loadAll() {
		Query query = manager.createQuery("from Filme");

		@SuppressWarnings("unchecked")
		Collection<Filme> resultList = query.getResultList();

		return resultList;
	}
	
	@Override
	public Filme loadById(Long id) {
		return manager.find(Filme.class, id);
	}

	@Override
	public void remove(Filme filme) {
	    manager.remove(manager.getReference(Filme.class, filme.getId()));
	}

	@Override
	public Filme save(Filme filme) {
		return manager.merge(filme);
	}

}
